package ytdp.careagle.com.ytdp.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import ytdp.careagle.com.ytdp.activity.BaseActivity;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.callback.MyPromissCallback;

/**
 * Created by admin on 2017/9/5.
 */

public class RxPromisUtils {
    public void cameraPromis(final Activity activity, final Intent intent) {
        new RxPermissions(activity)
                .request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) {
                        try {
                            Camera camera = Camera.open(G.CAMERA_FRONT_OR_BACK);
                            camera.release();
                            camera = null;
                        } catch (Exception e) {
                            e.printStackTrace();
                            MyToast.toast("请开启相关权限");
                            return;
                        }
                        if (aBoolean) {
                            activity.startActivity(intent);
                        } else {
                            MyToast.toast("请到设置中同意相关权限");
                        }
                    }
                });

    }

    public void voicePromis(final BaseActivity activity, final MyPromissCallback callback) {
        new RxPermissions(activity)
                .request(Manifest.permission.RECORD_AUDIO)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) {
                        if (aBoolean) {
                            callback.callback(true);
                        } else {
                            callback.callback(false);
                            activity.showMessage("请到设置中同意相关权限");
                        }
                    }
                });

    }
}
